<template>
    <div>
      <h1>登陆</h1>
      <form action="" @submit.prevent="on_submit">
        用户名:<input name="username" v-model="username">
        密码<input name="password" v-model="password">
        <input type="submit" value="登陆">
      </form>
    </div>
</template>

<script>
  import axios from 'axios'
    export default {
        name: "Login",
      data:function () {
        return{
          username:"",
          password:''
        }
      },
      methods:{
          on_submit(){
            axios({
              method:'post',
              url:'http://127.0.0.1:8000/login/',
              data:{
                username: this.username,
                password: this.password,
              }
            }).then(res=>{
              let code=res['data']['code']
              console.log(res.data)
              if (code==201){
                alert('输入不完整')
              }
              else if (code==202){
                alert('账号密码错误')
              }
              else {
                this.$router.push({'name':'Index'})
                sessionStorage.setItem('name',res.data.username)
              }
            })
          }
      }
    }
</script>

<style scoped>

</style>
